<?php //$Rev:: 58         $ // DO NOT REMOVE

error_reporting(E_ALL);

/**
 * ATACModule: Loads modules (callbacks) and sets them for use interactively
 * 
 * @author Andrew Heebner <andrew@evilwalrus.com> 
 * @version $Revision: 58 $
 * @require PHP 5.0
 */
class ATACModule
{

    public $core;

	/**
	 * __construct: ATACModule constructor
	 * 
	 * @param object $object Object to store for parent referencing
	 * @access public 
	 * @return void 
	 */
	public function __construct(ATAC $object)
	{
		$this->core = $object;
        $this->aim_loadAllModules($this->core->user['modules']);
	}

	/**
	 * __destruct: ATACModule destructor
	 * 
	 * @access public 
	 * @return void 
	 */
	public function __destruct()
	{
		unset($this->core);
	} 

	/**
	 * __call: ATACModule invalid method parser
	 * 
	 * @param string $method Invalid method called
	 * @access private 
	 * @return void 
	 * @throws ATACException
	 */
	public function __call($method, $args)
	{
		throw new ATACException('Invalid method called => (' . __CLASS__ . '::' . $method . ')');
	}

	/**
	 * aim_loadAllModules: Load *all* modules exclusively in a directory
	 * 
	 * @access public 
	 * @return void 
	 * @proto void aim_loadAllModules(void)
	 */
    public function aim_loadAllModules()
    {
        $c = 0;
        $list = array();
        if (is_dir($this->core->user['modules'])) {
            // we allow subdirectories!
            if ($files = glob($this->core->user['modules'] . '/*/mod_*.php')) {
                // reset the array to nothing, duh?
                $this->core->handlers = array();
                foreach ($files as $f) {
                    $this->aim_loadModule($f, true);
                    $c++;
                }
                return $c;
            }
        } else {
            throw new ATACException('Module directory was not found');
        }
    }

	/**
	 * aim_loadModule: Load module exclusively one by one
     *
	 * @access public 
	 * @return void 
	 * @proto void aim_loadModule(string module [, boolean from_class])
	 */
    public function aim_loadModule($modFile, $fromClass = false)
    {
        $modFile = (!$fromClass) ? $this->core->user['modules'] . $modFile : $modFile;
        if (is_file($modFile)) {
            // let the magic begin!!!!!!!!!!!!
            if (false !== ($module = Tokenizer::tokenize_module($modFile))) {
                foreach ($module as $key => $val) {
                    if (array_key_exists($key, $this->core->handlers)) {
                        unset($this->core->handlers[$key]);
                    }
                    $this->core->handlers[$key] = $val;
                    $this->core->handlers[$key]['file'] = $modFile;
                }
                $this->aim_updateModuleInfo();
            } elseif (strtolower(basename($modFile)) == 'mod_standard.php') {
                // this is for all of you "13370 h4><0r5" out there, beware of the sorcery *crowd applaud*
                //
                // in essence, if you remove, modify, or hump
                // mod_standard.php the wrong way, ATAC essentially
                // says "FUCK YOU", but in nicer terms
                throw new ATACException("The module 'mod_standard.php' is required by ATAC to run.  The code appears to be malformed; Please consult your modules directory to fix the problem.");
            } else {
                $this->core->aim_debug("The code contained in '".$modFile."' appears to be malformed.  Module not loaded. ", AIM_WARN);
            }
        } else {
            $this->core->aim_debug(sprintf("The requested file '%s' could not be found", $modFile), AIM_WARN);
        }
    }

	/**
	 * aim_updateModuleInfo: Load module information into our buddy information
     *
	 * @access public 
	 * @return void 
	 * @proto void aim_updateModuleInfo(void)
	 */
    public function aim_updateModuleInfo()
    {
        $modules = join(', ', array_keys($this->core->handlers));
        $this->core->utilities->aim_set_info($this->core->user['info'] . "<hr><b>".count(array_keys($this->core->handlers))."</b> command(s) loaded: " . $modules);
    }

	/**
	 * aim_unloadCommand: Unload command (block public from using it)
     *
	 * @access public 
	 * @return void 
	 * @proto void aim_unloadCommand(string command_name)
	 */
    public function aim_unloadCommand($cmd)
    {
        if (array_key_exists($cmd, $this->core->handlers)) {
            unset($this->core->handlers[$cmd]);
            $this->aim_updateModuleInfo();
        }
    }

    public function aim_help_requested($data)
    {
        if ($data['message_text'] == $this->core->user['help_cmd']) {
            if (array_key_exists($data['message_alert'], $this->core->handlers)) {
                $loc = pathinfo($this->core->handlers[$data['message_alert']]['file']);
                $file = $loc['dirname'] . '/help/' . $data['message_alert'] . '.txt';
                $text = @file_get_contents($file);
                return ($text !== false) ? $text : 'There is no help file for "'.$data['message_alert'].'"';
            }
        }
        return false;
    }

} // End ATACModule

?>